package com.kgc.basicprovider.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.kgc.basic.pojo.Provider;
import com.kgc.basic.util.PageInfo;
import com.kgc.basic.util.ResponseMsg;
import com.kgc.basicprovider.dao.ProviderMapper;
import com.kgc.basicprovider.service.ProviderService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;


@Service
public class ProviderServiceImpl implements ProviderService {

    private Logger logger = LoggerFactory.getLogger(ProviderServiceImpl.class);

    @Autowired
    private ProviderMapper providerMapper;


    @Override
    public ResponseMsg selectProviderByCode(String code) {
        logger.info("ProviderServiceImpl selectProviderByCode code:{}", code);
        ResponseMsg msg = new ResponseMsg();
        Provider provider = providerMapper.selectProviderByCode(code);
        if (provider != null) {
            msg.setCode("301");
            msg.setMsg("供应商编码不能重复");
            return msg;
        }
        msg.setCode("200");
        return msg;
    }

    @Override
    public ResponseMsg selectProviderByPage(Map<String, Object> map) {
        logger.info("ProviderServiceImpl selectProviderByPage params,{}", map);
        ResponseMsg responseMsg = new ResponseMsg();
        PageInfo<Provider> pageInfo = new PageInfo<>();
        pageInfo.setPageNo(map.get("pageNo") == null ? 1 : Integer.parseInt(map.get("pageNo").toString()));
        pageInfo.setPageSize(map.get("pageSize") == null ? 10 : Integer.parseInt(map.get("pageSize").toString()));
        Page<Object> page = PageHelper.startPage(pageInfo.getPageNo(), pageInfo.getPageSize(), "createDate desc");
        List<Provider> ProviderList = providerMapper.selectProviderByPage(map);
        pageInfo.setList(ProviderList);
        pageInfo.setTotal((int) page.getTotal());
        responseMsg.setData(pageInfo);
        logger.debug("ProviderServiceImpl selectProviderByPage result:{}", responseMsg);
        return responseMsg;
    }

    @Override
    public ResponseMsg insertProvider(Provider provider) {
        logger.info("ProviderServiceImpl insertProvider params,{}", provider);
        ResponseMsg responseMsg = new ResponseMsg();
        provider.setCreateDate(new Date());
        provider.setUpdateDate(new Date());
        int result = providerMapper.insertProvider(provider);
        if (result > 0) {
            responseMsg.setCode("200");
        } else {
            responseMsg.setCode("201");
        }
        logger.debug("ProviderServiceImpl insertProvider result:{}", responseMsg);
        return responseMsg;
    }

    @Override
    public ResponseMsg updateProvider(Provider provider) {
        logger.info("ProviderServiceImpl updateProvider params,{}", provider);
        ResponseMsg responseMsg = new ResponseMsg();
        provider.setUpdateDate(new Date());
        int result = providerMapper.updateProvider(provider);
        if (result > 0) {
            responseMsg.setCode("200");
        } else {
            responseMsg.setCode("201");
        }
        logger.debug("ProviderServiceImpl updateProvider result:{}", responseMsg);
        return responseMsg;
    }

    @Override
    public ResponseMsg deleteProvider(int id) {
        logger.info("ProviderServiceImpl deleteProvider params,{}", id);
        ResponseMsg responseMsg = new ResponseMsg();
        int result = providerMapper.deleteProvider(id);
        if (result > 0) {
            responseMsg.setCode("200");
        } else {
            responseMsg.setCode("201");
        }
        logger.debug("ProviderServiceImpl deleteProvider result:{}", responseMsg);
        return responseMsg;
    }

    @Override
    public ResponseMsg selectAllSupplier() {
        logger.info("ProviderServiceImpl selectAllSupplier is start...");
        ResponseMsg msg = new ResponseMsg();
        List<Provider> providerList = providerMapper.selectAllSupplier();
        if (providerList == null || providerList.isEmpty()) {
            msg.setCode("301");
            msg.setMsg("未查询到相关信息");
            return msg;
        }
        msg.setCode("200");
        msg.setData(providerList);
        return msg;
    }

    @Override
    public ResponseMsg getProviderAllByData() {
        logger.info("ProviderServiceImpl getProviderAllByData is start...");
        ResponseMsg msg = new ResponseMsg();
        Map<String, Object> providerAllByDataMap = providerMapper.getProviderAllByData();
        if (providerAllByDataMap == null || providerAllByDataMap.isEmpty()) {
            msg.setCode("301");
            msg.setMsg("未查询到相关信息");
            return msg;
        }
        msg.setCode("200");
        msg.setData(providerAllByDataMap);
        return msg;
    }
}
